const {
  userFormDataError,
  userRepetitionError,
  userCreateError,
  userGetInfoError,
  userLoginError,
  userChangePasswordError,
  authExpiredError,
  authInvalidError,
  questionCreateError,
  questionGetInfoError,
  questionPublishedError,
} = require("../constant/errorType");

module.exports = (error = {}, ctx) => {
  let message;
  switch (error.code) {
    case 10001:
      message = userFormDataError.message;
      console.error(`${message}==>`, ctx.request.body);
      break;

    case 10002:
      message = userRepetitionError.message;
      console.error(`${message}==>`, ctx.request.body);
      break;

    case 10003:
      message = userCreateError.message;
      console.error(`${message}==>`, ctx.request.body);
      break;

    case 10004:
      message = userGetInfoError.message;
      console.error(`${message}==>`, ctx.request.body);
      break;

    case 10005:
      message = userLoginError.message;
      console.error(`${message}==>`, ctx.request.body);
      break;

    case 10006:
      message = userChangePasswordError.message;
      console.error(`${message}==>`, ctx.request.body);
      break;

    case 10101:
      message = authExpiredError.message;
      console.error(`${message}==>`, ctx.request.body);
      break;

    case 10102:
      message = authInvalidError.message;
      console.error(`${message}==>`, ctx.request.body);
      break;

    case 10201:
      message = questionCreateError.message;
      console.error(`${message}==>`, ctx.request.body);
      break;

    case 10202:
      message = questionGetInfoError.message;
      console.error(`${message}==>`, ctx.request.body);
      break;

    case 10203:
      message = questionPublishedError.message;
      console.error(`${message}==>`, ctx.request.body);
      break;

    default:
      console.error("服务器内部错误==>", error);
      break;
  }
  ctx.body = { ...error, message };
};
